
//设置设备基本参数
import 'dart:io';

import 'package:ble2301/ble2301_plugin.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:toast/toast.dart';

import '../util/app_all_value.dart';
import '../util/event_bus.dart';

class DeviceInformationSettingPage extends StatefulWidget{

  const DeviceInformationSettingPage({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => _deviceInformationSettingState();
}

class _deviceInformationSettingState extends State<DeviceInformationSettingPage>{
  AppAllValueController controller = Get.find();
  int selectHandle =0; //0：左手 1：右手
  bool power = false;
  bool agreement =false;
  int scent =0;//0：蓝牙 1：HID
  bool sportMode = false;
  int deviceType =Platform.isAndroid? 1:0; //0：IOS 1：android
  bool encryption =false;
  bool autoCheck=false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("设置设备基本信息"),),
      body: ListView.builder(itemBuilder: (contetx,index){
        if(index==0){
          return Row(children: [
            Text("电源开关："),
            Switch(value: power, onChanged: (value){
              setState(() {
                power = value;
              });
            })
          ],);
        }else if(index==1){
          return Row(children: [
            Text("运动模式是否开灯："),
            Switch(value: sportMode, onChanged: (value){
              setState(() {
                sportMode = value;
              });
            })
          ],);
        }else if(index==2){
          return Row(children: [
            Text("加密："),
            Switch(value: encryption, onChanged: (value){
              setState(() {
                encryption = value;
              });
            })
          ],);
        }else if(index==3){
          return Row(children: [
            Text("自动检测运动："),
            Switch(value: autoCheck, onChanged: (value){
              setState(() {
                autoCheck = value;
              });
            })
          ],);
        }else if(index==4){
          return Row(
            children: [
              Text("左右手标志："),
              Radio(
                value: 0,
                groupValue: selectHandle,
                onChanged: (value) {
                  setState(() {
                    selectHandle = value as int;
                  });
                },
              ),
              Text("左手"),
              Radio(
                value: 1,
                groupValue: selectHandle,
                onChanged: (value) {
                  setState(() {
                    selectHandle = value as int;
                  });
                },
              ),
              Text("右手")
            ],
          );
        }else if(index==5){
          return Row(
            children: [
              Text("触摸场景："),
              Radio(
                value: 0,
                groupValue: scent,
                onChanged: (value) {
                  setState(() {
                    scent = value as int;
                  });
                },
              ),
              Text("蓝牙发送原始键值"),
              Radio(
                value: 1,
                groupValue: scent,
                onChanged: (value) {
                  setState(() {
                    scent = value as int;
                  });
                },
              ),
              Text("HID控制模式")
            ],
          );
        }else if(index==6){
          return Row(
            children: [
              Text("手机类型："),
              Radio(
                value: 1,
                groupValue: deviceType,
                onChanged: (value) {
                  setState(() {
                    deviceType = value as int;
                  });
                },
              ),
              Text("安卓"),
              Radio(
                value: 0,
                groupValue: deviceType,
                onChanged: (value) {
                  setState(() {
                    deviceType = value as int;
                  });
                },
              ),
              Text("苹果")
            ],
          );
        }else if(index==7){
          return Container(
            margin: const EdgeInsets.only(right: 10),
            child: ElevatedButton(
              child: Text('设置'),
              onPressed: () {
                sendSettingDevice();
              },
            ),
          );
        }
        },shrinkWrap: false,
        itemCount: 8,
      ),
    );
  }

  void sendSettingDevice(){
    MyDeviceInfo deviceInfoV2=MyDeviceInfo(handle: selectHandle,
        power: power,
        agreement: agreement,
        scent: scent,
        sportMode: sportMode,
        device: deviceType,
        encryption: encryption,
        autoCheck: autoCheck
    );
    controller.writeData(BleSDK.SetDeviceInfo(deviceInfoV2));
  }

  @override
  void dispose() {
    super.dispose();
    EventBus().off('dataCallBack');
  }

  @override
  void initState() {
    super.initState();
    EventBus().on('dataCallBack', (arg){
      Map map = arg;
      bool finish = map[DeviceKey.End];
      switch (map[DeviceKey.DataType] as String) {
        case BleConst.SetDeviceInfo:
          if(finish){
            controller.dismissDialog();
            Toast.show("设置成功");
          break;
      }}}
    );
  }

}